package classDemo;
import java.util.*;

public class Classroom {
    private final String className;
    private Student[] students;
    private int size;

    public Classroom(String className) {
        this.className = className;
        this.students = new Student[10];
        this.size = 0;
    }

    void register(Student student) {
        if (size == students.length) {
            // 空间不够时，数组扩容
            Student[] newStudents = new Student[students.length * 2];
            System.arraycopy(students, 0, newStudents, 0, students.length);
            students = newStudents;
        }
        students[size++] = student;
    }

    public void printStudentsSortedByScore() {
        Student[] sortedStudents = Arrays.copyOf(students, size);
        Arrays.sort(sortedStudents, (s1, s2) -> {
            if (s2.getTotalScore() != s1.getTotalScore()) {
                return Double.compare(s2.getTotalScore(), s1.getTotalScore());
            } else if (s2.getChineseScore() != s1.getChineseScore()) {
                return Double.compare(s2.getChineseScore(), s1.getChineseScore());
            } else if (s2.getMathScore() != s1.getMathScore()) {
                return Double.compare(s2.getMathScore(), s1.getMathScore());
            } else if (s2.getEnglishScore() != s1.getEnglishScore()) {
                return Double.compare(s2.getEnglishScore(), s1.getEnglishScore());
            } else {
                return s2.getName().compareTo(s1.getName());
            }
        });

        for (Student student : sortedStudents) {
            System.out.println(student);
        }
    }
}


